package com.audaque.springboot.foshanupload.jwtvalid.config;

import com.audaque.springboot.foshanupload.authcore.properties.LoginProperties;
import com.audaque.springboot.foshanupload.core.constants.ComConst;
import com.audaque.springboot.foshanupload.core.properties.StaticResourcePathProperties;
import com.audaque.springboot.foshanupload.jwtvalid.filter.JwtBodyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * @author zgb
 * @desc filter用于认证和授权
 * @date 2022-01-25 22:24:15
 */
@Configuration
public class JwtFilterConfig {

    @Autowired
    private LoginProperties loginProperties;
    @Autowired
    private JwtBodyFilter jwtBodyFilter;
    @Autowired
    private StaticResourcePathProperties staticResourcePathProperties;


    /**
     * 配置过滤器
     * order属性:控制过滤器加载顺序：数字越小，加载越早
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean jwtBodyFilterRegistration() {
        //新建过滤器注册类
        FilterRegistrationBean<JwtBodyFilter> registration = new FilterRegistrationBean<JwtBodyFilter>();
        registration.setFilter(jwtBodyFilter);
        registration.setName("jwtBodyFilter");
        // 设置过滤器的URL模式
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);

        //要排除的路径
        List<String > excludePathPatterns = staticResourcePathProperties.getList();
        List<String> unAuthenticatedUrlList = loginProperties.getUnAuthenticatedUrlList();
        unAuthenticatedUrlList.addAll(excludePathPatterns);
        String excludedUris = String.join(",", unAuthenticatedUrlList);
        //固定参数
        registration.addInitParameter("excludedUris", excludedUris);

        //控制过滤器加载顺序：数字越小，加载越早
        registration.setOrder(0);
        return registration;
    }


}
